#include "stdafx.h"

const UINT WMR_UPDATE_LID    = RegisterWindowMessage( L"Zive.PanicButton.Private.TestClient2.UpdateLid" );
const UINT WMR_UPDATE_BUTTON = RegisterWindowMessage( L"Zive.PanicButton.Private.TestClient2.UpdateButton" );

BEGIN_MESSAGE_MAP( CTestClient2Dlg, CDialogEx )
	ON_WM_SYSCOMMAND( )
	ON_WM_PAINT( )
	ON_WM_QUERYDRAGICON( )
	ON_WM_CLOSE( )
	ON_REGISTERED_MESSAGE( WMR_UPDATE_LID, OnUpdateLid )
	ON_REGISTERED_MESSAGE( WMR_UPDATE_BUTTON, OnUpdateButton )
	ON_BN_CLICKED( IDOK, &CTestClient2Dlg::OnOk )
	ON_BN_CLICKED( IDCANCEL, &CTestClient2Dlg::OnCancel )
END_MESSAGE_MAP( )

CTestClient2Dlg::CTestClient2Dlg( CComPtr<IPanicButton>& pPanicButton, CComPtr<IPanicButtonEventSink2> pPanicButtonEventSink2, CWnd* pParent ):
	CDialogEx( CTestClient2Dlg::IDD, pParent )
{
	m_pPanicButton = pPanicButton;
	m_pPanicButtonEventSink = pPanicButtonEventSink2;

	m_hIcon = AfxGetApp( )->LoadIcon( IDR_MAINFRAME );
	m_strLidStatus = L"closed";
	m_strButtonStatus = L"up";
}

bool CTestClient2Dlg::_SetUpPanicButton( void ) {
	m_pPanicButtonEventSink->put_NotifyWindow( m_hWnd );

	HRESULT hr = m_pPanicButton->OpenDevice( );
	if ( FAILED( hr ) ) {
		BreakIfDebuggerPresent( );
		return false;
	}
	return true;
}

void CTestClient2Dlg::_TearDownPanicButton( void ) {
	HRESULT hr;

	hr = m_pPanicButton->CloseDevice( );
	if ( FAILED( hr ) ) {
		BreakIfDebuggerPresent( );
	}
}

void CTestClient2Dlg::DoDataExchange( CDataExchange* pDX ) {
	CDialogEx::DoDataExchange( pDX );
	DDX_Text( pDX, IDC_BUTTON_STATUS, m_strButtonStatus );
	DDX_Text( pDX, IDC_LID_STATUS, m_strLidStatus );
}

BOOL CTestClient2Dlg::OnInitDialog( ) {
	CDialogEx::OnInitDialog( );

	//
	// Add "About..." menu item to system menu.
	//

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT( ( IDM_ABOUTBOX & 0xFFF0 ) == IDM_ABOUTBOX );
	ASSERT( IDM_ABOUTBOX < 0xF000 );

	CMenu* pSysMenu = GetSystemMenu( FALSE );
	if ( pSysMenu ) {
		CString strAboutMenu( MAKEINTRESOURCE( IDS_ABOUTBOX ) );
		if ( !strAboutMenu.IsEmpty( ) ) {
			pSysMenu->AppendMenu( MF_SEPARATOR );
			pSysMenu->AppendMenu( MF_STRING, IDM_ABOUTBOX, strAboutMenu );
		}
	}

	//
	// Set the icon for this dialog. The framework does this automatically
	// when the application's main window is not a dialog.
	//

	SetIcon( m_hIcon, TRUE );
	SetIcon( m_hIcon, FALSE );

	//
	// Create panic button object and event sink, and connect them together
	//

	_SetUpPanicButton( );

	// return TRUE unless you set the focus to a control
	return TRUE;
}

void CTestClient2Dlg::OnSysCommand( UINT nID, LPARAM lParam ) {
	if ( ( nID & 0xFFF0 ) == IDM_ABOUTBOX ) {
		CAboutDlg dlgAbout;
		dlgAbout.DoModal( );
	} else {
		CDialogEx::OnSysCommand( nID, lParam );
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
void CTestClient2Dlg::OnPaint( ) {
	if ( IsIconic( ) ) {
		CPaintDC dc( this ); // device context for painting

		SendMessage( WM_ICONERASEBKGND, reinterpret_cast<WPARAM>( dc.GetSafeHdc( ) ), 0 );

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics( SM_CXICON );
		int cyIcon = GetSystemMetrics( SM_CYICON );

		CRect rect;
		GetClientRect( &rect );
		int x = ( rect.Width( )  - cxIcon + 1 ) / 2;
		int y = ( rect.Height( ) - cyIcon + 1 ) / 2;

		// Draw the icon
		dc.DrawIcon( x, y, m_hIcon );
	} else {
		CDialogEx::OnPaint( );
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestClient2Dlg::OnQueryDragIcon( ) {
	return static_cast<HCURSOR>( m_hIcon );
}

void CTestClient2Dlg::OnClose( ) {
	_TearDownPanicButton( );

	CDialogEx::OnClose( );
}

void CTestClient2Dlg::OnOk( ) {
	_TearDownPanicButton( );

	CDialogEx::OnOK( );
}

void CTestClient2Dlg::OnCancel( ) {
	_TearDownPanicButton( );

	CDialogEx::OnCancel( );
}

LRESULT CTestClient2Dlg::OnUpdateLid( WPARAM wParam, LPARAM lParam ) {
	debug( L"CTestClient2Dlg::OnUpdateLid: tid 0x%lX\n", GetCurrentThreadId( ) );
	m_strLidStatus = wParam ? L"OPEN" : L"closed";
	UpdateData( FALSE );
	return 0;
}

LRESULT CTestClient2Dlg::OnUpdateButton( WPARAM wParam, LPARAM lParam ) {
	debug( L"CTestClient2Dlg::OnUpdateButton: tid 0x%lX\n", GetCurrentThreadId( ) );
	m_strButtonStatus = wParam ? L"PRESSED" : L"up";
	UpdateData( FALSE );
	return 0;
}
